package hzau.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hzau.constants.PageConstant;
import hzau.constants.ResultConstant;
import hzau.dao.*;
import hzau.pojo.College;
import hzau.pojo.Course;
import hzau.pojo.Paper;
import hzau.pojo.question.Question;
import hzau.pojo.question.SelectionQuestion;
import hzau.pojo.requestParams.PaperParam;
import hzau.result.Result;
import hzau.service.PaperService;
import hzau.utils.JwtUtils;
import hzau.utils.WordCompareUtil;
import hzau.utils.WordGenUtil;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PaperServiceImpl implements PaperService {
    final SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final Integer pageSize=10;
    @Autowired
    CourseDao courseDao;
    @Autowired
    PaperDao paperDao;
    @Autowired
    QuestionDao questionDao;
    @Autowired
    SelectionQuestionDao selectionQuestionDao;
    @Autowired
    CollegeDao collegeDao;
    @Autowired
    WordGenUtil wordGenUtil;
    @Override
    public Result update(Paper paper) {
        Course course = courseDao.selectById(paper.getCourseId());
        if(course!=null){
            Date date=new Date(System.currentTimeMillis());

            paper.setLastAlterTime(ft.format(date));
            paperDao.updateById(paper);
        return Result.success(null);
        }
        else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COURSE);
    }

    @Override
    public Result register(PaperParam paper, String token) {
        Long id= JwtUtils.getUserId(token);
        Course course = courseDao.selectById(paper.getCourseId());
        if(course!=null){
            Paper paper1=new Paper();
            paper1.setParam(paper);

            paper1.setCreateId(id);

//            Date date=new Date(System.currentTimeMillis());
            Date date=new Date(System.currentTimeMillis());

            paper1.setLastAlterTime(ft.format(date));

            paperDao.insert(paper1);
            return Result.success(null);
        }
        else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COURSE);
    }

    @Override
    public Result delete(Long id) {
        paperDao.deleteById(id);
        return Result.success(null);
    }

    @Override
    public Result getAllQuestion(Long id) {
        List<Object> questionList=new ArrayList<>();
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getPaperId,id);
        LambdaQueryWrapper<SelectionQuestion> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(SelectionQuestion::getPaperId,id);
        List<Question> questions = questionDao.selectList(lambdaQueryWrapper);
        List<SelectionQuestion> selectionQuestions = selectionQuestionDao.selectList(lambdaQueryWrapper2);
        Collections.addAll(questionList,questions);
        Collections.addAll(questionList,selectionQuestions);
        return Result.success(questionList);

    }

    @Override
    public Result getPaper(Long id) {
        Paper paper = paperDao.selectById(id);
        return Result.success(paper);
    }

    @Override
    public Result updateState(Long id, Short state) {
        Paper paper = paperDao.selectById(id);
        paper.setState(state);
        Date date=new Date(System.currentTimeMillis());
        paper.setLastAlterTime(ft.format(date));
        paperDao.updateById(paper);
        return Result.success(null);
    }

    @Override
    public Result getByTeacherId(Long id,Integer  pageNum) {
        IPage<Paper> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Paper> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getCreateId,id);
        lambdaQueryWrapper.eq(Paper::getExpired,0);
        IPage<Paper> selectPage = paperDao.selectPage(page, lambdaQueryWrapper);
        List<Paper> papers =selectPage.getRecords();
        System.out.println(selectPage.getTotal());
        return Result.success(selectPage);
    }

    @Override
    public Result getById(Long id) {
        Paper paper = paperDao.selectById(id);
        return Result.success(paper);
    }

    @Override
    public Result getByReviewId(Long reviewId, Integer pageNum) {

        LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
        lqw.eq(College::getPresidentId,reviewId);
        List<College> collegeList = collegeDao.selectList(lqw);
        IPage<Paper> page;
        if(collegeList.size()==0)
        {page=new Page<>(pageNum,pageSize);}else page=new Page<>(pageNum,PageConstant.PAPER_PAGE_SIZE);
            LambdaQueryWrapper<Paper> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Paper::getReviewId,reviewId);
            lambdaQueryWrapper.eq(Paper::getExpired,0);
            lambdaQueryWrapper.eq(Paper::getState,1);
            IPage<Paper> selectPage = paperDao.selectPage(page, lambdaQueryWrapper);
            return Result.success(selectPage);


    }

    @Override
    public Result updateAdvice(String advice, Long paperId) {

        Paper paper = paperDao.selectById(paperId);
        paper.setAdvice(advice);
        paperDao.updateById(paper);
        return Result.success(null);
    }

    @Override
    public Result deletePaper(Long paperId) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getPaperId,paperId);
        questionDao.delete(lqw);
        LambdaQueryWrapper<SelectionQuestion> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SelectionQuestion::getPaperId,paperId);
        selectionQuestionDao.delete(lambdaQueryWrapper);
        paperDao.deleteById(paperId);
        return Result.success(null);
    }

    @Override
    public Result getPresidentReviewPaper(String token, Integer page) {
        Long id=JwtUtils.getUserId(token);
        IPage<Paper> pages=new Page<>(page, PageConstant.PAPER_PAGE_SIZE);
        LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
        lqw.eq(College::getPresidentId,id);
        List<College> collegeList = collegeDao.selectList(lqw);
        List<Long> idList=new ArrayList<>();
        for (College college : collegeList) {
            idList.add(college.getId());
        }
        LambdaQueryWrapper<Paper> lqw2=new LambdaQueryWrapper<>();
        lqw2.in(Paper::getCourseId,idList);
        lqw2.eq(Paper::getExpired,0);
        lqw2.eq(Paper::getState,2);
        IPage<Paper> page1 = paperDao.selectPage(pages, lqw2);
        return Result.success(page1);

    }

    @Override
    public Result presidentAdvice(String advice, Long paperId) {
        Paper paper = paperDao.selectById(paperId);
        paper.setMasterAdvice(advice);
        paperDao.insert(paper);
        return Result.success(null);
    }

    @Override
    public Result genPaper(Long paperId) throws Exception {
        String url= wordGenUtil.genWord(paperId);
        return Result.success(url);
    }

    @Override
    public Result getRecentYearPaper(Long paperId) {
        Paper paper = paperDao.selectById(paperId);
        LambdaQueryWrapper<Paper> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Paper::getCourseId,paper.getCourseId());
        lqw.lt(Paper::getStartYear,paper.getStartYear());
        lqw.eq(Paper::getExpired,1);
        //TODO 限制只有三年
        List<Paper> papers = paperDao.selectList(lqw);

        return Result.success(papers);
    }

    @Override
    public Result getRecentYearPaperSimilarity(Long paperId) {
        Paper paper = paperDao.selectById(paperId);
        LambdaQueryWrapper<Paper> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Paper::getCourseId,paper.getCourseId());
        lqw.lt(Paper::getStartYear,paper.getStartYear());
        lqw.eq(Paper::getExpired,1);
        //TODO 限制只有三年
        List<Paper> papers = paperDao.selectList(lqw);
        String info= WordCompareUtil.getInfo(paper.getUrl());
        for (Paper paper1 : papers) {
            String info2=WordCompareUtil.getInfo(paper1.getUrl());
            double similar = WordCompareUtil.computeTxtSimilar(info, info2);
            paper1.setSimilarity(similar);
        }

        return Result.success(papers);
    }


}
